use egui::{Margin, ScrollArea, Vec2};
use QcUI::core::context::UiContext;

use crate::{components::dock::DockView, core::context::Context};

use std::sync::Arc;

#[derive(Debug)]
pub struct ConsolePanel {
    context: Arc<Context>,
    message_list: Vec<String>,
}

impl DockView for ConsolePanel {
    fn render(&mut self, ctx: &mut UiContext, show_tab: bool) {
        let scroll_area = ScrollArea::new([true, true]);

        scroll_area.show(ctx.ui, |ui| {
            let mut odd = false;
            for msg in &self.message_list {
                let background_color = if odd {
                    egui::Color32::from_rgb(20, 20, 20)
                } else {
                    egui::Color32::from_rgb(43, 43, 43)
                };

                egui::Frame::none()
                    .inner_margin(egui::Margin::same(5.))
                    .fill(background_color)
                    .show(ui, |ui| {
                        ui.set_width(ui.available_width());
                        let text = egui::RichText::new(msg).color(egui::Color32::WHITE);
                        let label = egui::Label::new(text);
                        ui.add(label);
                    });

                odd = !odd;
            }
        });
    }
}

impl ConsolePanel {
    pub fn new(context: Arc<Context>) -> Self {
        Self {
            context,
            message_list: vec!["Quincy 游戏引擎".to_string()],
        }
    }
}
